#include "bsonlistmodel.h"
#include <QDebug>
#include "bsonexplorermodel.h"

BsonListModel::BsonListModel(QObject* parent)
    :QAbstractListModel(parent)
{
    clientConnection=NULL;
    cout<<"bsonListModel"<<this<<endl;
}
BsonListModel::~BsonListModel()
{
    cout<<"\nbsonListModel:desctructed..."<<endl;
}
int BsonListModel::rowCount(const QModelIndex &parent) const
{
    //return allNames.size();
    return allBSONObjs.size();
}

QVariant BsonListModel::data(const QModelIndex &index, int role) const
{
    if(!index.isValid())
        return QVariant();
    if(role==Qt::DisplayRole)
    {
        //BSONElement objIDElement;
        //allBSONObjs[index.row()].getObjectID(objIDElement);
        //return tr(objIDElement.toString().c_str());
//        if(index.row()<allBSONObjs.size())
//            return tr(allBSONObjs[index.row()][1].fieldName());
//        else
//            return QVariant();
//        if(index.row()<allBSONObjs.size())
//        {
//            BSONElement first=allBSONObjs[index.row()].firstElement();
//            return first.fieldName();
//        }else
//        {
//            return QVariant();
//        }
        return tr("Document:%1").arg(index.row());
        //return tr(allBSONObjs[index.row()]["_id"].toString().c_str());
    }
    return QVariant();
}

QVariant BsonListModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role != Qt::DisplayRole)
             return QVariant();
     if (orientation == Qt::Horizontal)
         return QString("Column %1").arg(section);
     else if(orientation==Qt::Vertical)
         return QString("Row %1").arg(section);
     return QVariant();
}

/**
 * \fn void BsonListModel::setSelectedCollection(DBClientConnection* clientConnection,string collectionName)
 * \brief 设置选中的集合
 *
 */
void BsonListModel::setSelectedCollection(DBClientConnection* clientConnection,string collectionName)
{
    cout<<"\nfunction:selectedCollection changed called...{"<<endl;
    try{
        allBSONObjs.clear();
        cout<<"\n-----------collection:"<<collectionName<<"----------"<<endl;
//        allNames.clear();
//        allNames.push_back("test1");
//        allNames.push_back("test2");
//        allNames.push_back("test3");
//        allNames.push_back("test4");
        if(!this->clientConnection)
            this->clientConnection=clientConnection;
        this->collectionName=collectionName;
        auto_ptr<DBClientCursor> cursor=clientConnection->query(collectionName,BSONObj());
        while(cursor->more())
        {
            //qDebug()<<"allBSONObjs.string"<<cursor->next().toString().c_str()<<"\n";
            BsonListModel::allBSONObjs.push_back(cursor->next());
            qDebug()<<"allBSONObjs["<<BsonListModel::allBSONObjs.size()-1<<"].md5="<<allBSONObjs[allBSONObjs.size()-1].md5().c_str()<<"\n";
            //cout<<__FUNCTION__<<"\t"<<allBSONObjs[allBSONObjs.size()-1];
        }
    }
    catch(DBException& e)
    {
        qDebug()<<__FUNCTION__<<e.what()<<"\n";
    }
    cout<<"}"<<endl;
    reset();
}

void BsonListModel::selectedBSONObjChanged(QModelIndex index)
{
    int i=-1;
    BSONObj selectedObj;
    try{
        auto_ptr<DBClientCursor> cursor=clientConnection->query(collectionName,BSONObj());
        while(cursor->more())
        {
            //qDebug()<<"allBSONObjs.string"<<cursor->next().toString().c_str()<<"\n";
            //allBSONObjs.push_back(cursor->next());
            i++;
            selectedObj=cursor->next();
            cout<<"allBSONObjs["<<i<<"].md5="<<selectedObj<<"\n";
            if(i>=index.row())
            {
                break;
            }
        }
        emit addBSONObjTableView(selectedObj);
    }catch(...)
    {
        qDebug()<<"\n"<<__FUNCTION__<<" i="<<i<<"\n";
    }
    cout<<"}"<<endl;
}

void BsonListModel::printBSONObjs()
{
    for(int i=0;i<allBSONObjs.size();i++)
    {
        cout<<"allBSONObjs["<<i<<"].md5="<<BsonListModel::allBSONObjs[i].md5()<<"\n";
        //cout<<__FUNCTION__<<"\t"<<bsonListModel->allBSONObjs[i];
    }
}
